Computer Science Related Others Courses AvailableThe Best Codder.blogspot.com

Write a Java program that implements a multithreaded program that has three threads. First thread generates a random integer every 1 second and if the value is even, the second thread computes the square of the number and prints. If the value is odd the third thread will print the value of the cube of the number.

Write a Java program that implements a multithreaded program that has three threads. First thread generates a random integer every 1 second and if the

Write a Java program that implements a multithreaded program that has three threads. First thread generates a random integer every 1 second and if the value is even, the second thread computes the square of the number and prints. If the value is odd the third thread will print the value of the cube of the number.

  1. import java.util.Random;

  2. public class Main {
  3.     public static void main(String[] args) {
  4.         // Create the shared data object
  5.         SharedData sharedData = new SharedData();
  6.         
  7.         // Create and start the three threads
  8.         Thread t1 = new Thread(new RandomNumberGenerator(sharedData));
  9.         Thread t2 = new Thread(new SquareCalculator(sharedData));
  10.         Thread t3 = new Thread(new CubePrinter(sharedData));
  11.         t1.start();
  12.         t2.start();
  13.         t3.start();
  14.     }
  15. }

  16. // A class to hold the shared data between the threads
  17. class SharedData {
  18.     private int number;
  19.     private boolean numberGenerated;
  20.     private boolean squareCalculated;
  21.     private boolean cubePrinted;
  22.     
  23.     public synchronized int getNumber() {
  24.         while (!numberGenerated) {
  25.             try {
  26.                 wait();
  27.             } catch (InterruptedException e) {
  28.                 e.printStackTrace();
  29.             }
  30.         }
  31.         numberGenerated = false;
  32.         notifyAll();
  33.         return number;
  34.     }
  35.     
  36.     public synchronized void setNumber(int number) {
  37.         while (numberGenerated) {
  38.             try {
  39.                 wait();
  40.             } catch (InterruptedException e) {
  41.                 e.printStackTrace();
  42.             }
  43.         }
  44.         this.number = number;
  45.         numberGenerated = true;
  46.         notifyAll();
  47.     }
  48.     
  49.     public synchronized boolean isSquareCalculated() {
  50.         return squareCalculated;
  51.     }
  52.     
  53.     public synchronized void setSquareCalculated(boolean squareCalculated) {
  54.         this.squareCalculated = squareCalculated;
  55.     }
  56.     
  57.     public synchronized boolean isCubePrinted() {
  58.         return cubePrinted;
  59.     }
  60.     
  61.     public synchronized void setCubePrinted(boolean cubePrinted) {
  62.         this.cubePrinted = cubePrinted;
  63.     }
  64. }

  65. // The first thread that generates a random integer every second
  66. class RandomNumberGenerator implements Runnable {
  67.     private SharedData sharedData;
  68.     private Random random;
  69.     
  70.     public RandomNumberGenerator(SharedData sharedData) {
  71.         this.sharedData = sharedData;
  72.         this.random = new Random();
  73.     }
  74.     
  75.     public void run() {
  76.         while (true) {
  77.             int number = random.nextInt(100);
  78.             System.out.println("Generated number: " + number);
  79.             sharedData.setNumber(number);
  80.             try {
  81.                 Thread.sleep(1000);
  82.             } catch (InterruptedException e) {
  83.                 e.printStackTrace();
  84.             }
  85.         }
  86.     }
  87. }

  88. // The second thread that calculates the square of the number if it is even
  89. class SquareCalculator implements Runnable {
  90.     private SharedData sharedData;
  91.     
  92.     public SquareCalculator(SharedData sharedData) {
  93.         this.sharedData = sharedData;
  94.     }
  95.     
  96.     public void run() {
  97.         while (true) {
  98.             int number = sharedData.getNumber();
  99.             if (number % 2 == 0) {
  100.                 int square = number * number;
  101.                 System.out.println("Square of " + number + ": " + square);
  102.                 sharedData.setSquareCalculated(true);
  103.             }
  104.         }
  105.     }
  106. }

  107. // The third thread that prints the cube of the number if it is odd
  108. class CubePrinter implements Runnable {
  109.     private SharedData sharedData;
  110.     
  111.     public CubePrinter(SharedData sharedData) {
  112.         this.sharedData = sharedData;
  113.     }
  114.     
  115.     public void run() {
  116.         while (true) {
  117.             int number = sharedData.getNumber();
  118.             if (number % 2 != 0) {
  119.                 int cube = number * number * number;
  120.                 System.out.println("Cube of " + number + ": " + cube);
  121.                 sharedData.setCubePrinted(true);
  122.             }
  123.         }
  124.     }
  125. }

Givan by -Ujjwal Matoliya

Post a Comment

© JAVA. The Best Codder All rights reserved. Distributed by